package com.yiren.manager.ctrl.pension;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.StringUtil;
import com.yiren.core.exception.MyException;
import com.yiren.core.exception.SwallowException;
import com.yiren.core.utils.Favorites;
import com.yiren.core.utils.HTTPResult;
import com.yiren.core.utils.JsonMapperTool;
import com.yiren.core.utils.UUIDGenerator;
import com.yiren.entity.YrNursingCost;
import com.yiren.entity.YrNursingCostHistory;
import com.yiren.manager.ctrl.GenericController;
import com.yiren.manager.form.pension.YrNursingCostForm;
import com.yiren.manager.service.iface.pension.YrNursingCostService;
import com.yiren.manager.utils.ObjectUtils;

@RequestMapping("nursingCost")
@Controller
public class YrNursingCostController extends GenericController<YrNursingCostForm, YrNursingCost>{

	@Autowired
	private YrNursingCostService yrNursingCostService;
	
	//保存实体
	@RequestMapping("/save")
	@ResponseBody
	public HTTPResult<YrNursingCostForm> save(String data) throws SwallowException, MyException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException{
		HTTPResult<YrNursingCostForm> result = new HTTPResult<YrNursingCostForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		YrNursingCostForm form = tool.mapper("nursingCost", YrNursingCostForm.class);
		if(null==form){
			throwExp("param.error");
		}
		if(null==form.getNursingCostId()||form.getNursingCostId().isEmpty()){
			form.setNursingCostId(UUIDGenerator.getUUID());
			form.setCreateTime(new Date());
		}
		form.setUpdateTime(new Date());
		
		//实体
		YrNursingCost entity = ObjectUtils.objectToObject(form, YrNursingCost.class);
		//修改历史
		YrNursingCostHistory nursingCostHistory = ObjectUtils.objectToObject(entity,YrNursingCostHistory.class);
		
		Favorites favorites = new Favorites();
		favorites.putFavorite(YrNursingCostHistory.class, nursingCostHistory);
		favorites.putFavorite(YrNursingCost.class, entity);
		
		yrNursingCostService.addByFavorites(favorites);
		
		return result;
	}
	
	/**获取所有记录*/
	@RequestMapping("findAll")
	@ResponseBody
	public HTTPResult<YrNursingCostForm> findAll() throws MyException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
		HTTPResult<YrNursingCostForm> result = new HTTPResult<YrNursingCostForm>();
		List<YrNursingCost> entityList = yrNursingCostService.findAll();
		List<YrNursingCostForm> formList = new ArrayList<YrNursingCostForm>();
		for(YrNursingCost nursingCost : entityList){
			YrNursingCostForm form = ObjectUtils.objectToObject(nursingCost, YrNursingCostForm.class);
			formList.add(form);
		}
		result.setData(formList);
		return result;
	}
	
	/**删除一条记录*/
	@RequestMapping("delete")
	@ResponseBody
	public HTTPResult<YrNursingCostForm> delete(String id) throws MyException{
		HTTPResult<YrNursingCostForm> result = new HTTPResult<YrNursingCostForm>();
		if(StringUtil.isEmpty(id)){
			throwExp("param.error");
		}
		yrNursingCostService.delete(id);
		return result;
	}
	
	/**获取一条记录*/
	@RequestMapping("findOne")
	@ResponseBody
	public HTTPResult<YrNursingCostForm> findOne(String id) throws MyException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
		HTTPResult<YrNursingCostForm> result = new HTTPResult<YrNursingCostForm>();
		if(StringUtil.isEmpty(id)){
			throwExp("param.error");
		}
		YrNursingCost entity = yrNursingCostService.findOne(id);
		List<YrNursingCostForm> formList = new ArrayList<YrNursingCostForm>();
		
		YrNursingCostForm form =  ObjectUtils.objectToObject(entity, YrNursingCostForm.class);

		formList.add(form);
		
		result.setData(formList);
		return result;
	}
	
	
	/**
	 * 批量删除
	 * @param data
	 * @return
	 * @throws SwallowException
	 * @throws MyException
	 */
	@RequestMapping("deleteBatch")
	@ResponseBody
	public HTTPResult<YrNursingCostForm> deleteBatch(String data) throws SwallowException, MyException{
		HTTPResult<YrNursingCostForm> result = new HTTPResult<YrNursingCostForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		String[] ids = tool.mapper("ids", String[].class);
		if(null==ids){
			throwExp("param.error");
		}
		for(String id : ids){
			yrNursingCostService.delete(id);
		}
		return result;
	}
	
	@Override
	public YrNursingCostForm toForm(YrNursingCost e) throws Exception {
		YrNursingCostForm form = ObjectUtils.objectToObject(e, YrNursingCostForm.class);
		return form;
	}

	@Override
	public YrNursingCost toEntity(YrNursingCostForm f) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
